package vip.websky.project.web.controller.user;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.EditRoleReqDTO;
import vip.websky.project.module.dto.request.user.GetRoleReqDTO;
import vip.websky.project.user.service.RoleService;
import vip.websky.project.web.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/26-21:44
 **/
@Slf4j
@Controller
@RequestMapping("/role/*")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = {"toList"}, method = RequestMethod.GET)
    public String toList() {
        return "/user/roleList";
    }

    @RequestMapping(value = {"toEdit"}, method = RequestMethod.GET)
    public String toEdit() {
        return "/user/roleEdit";
    }

    @RequestMapping(method = RequestMethod.POST)
    public void editRole(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                     EditRoleReqDTO roleReqDTO) {
        String param = JSON.toJSONString(roleReqDTO);
        log.info("调用[RoleController-edit]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data;
        if (roleReqDTO.getId() == null || roleReqDTO.getId().equals("")) {
            data = roleService.addRole(requestDTO);
        } else {
            data = roleService.editRole(requestDTO);
        }
        sendMessages(request, response, data);
    }

    @RequestMapping(method = RequestMethod.GET)
    public void getRoleList(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                     GetRoleReqDTO roleReqDTO) {
        String param = JSON.toJSONString(roleReqDTO);
        log.info("调用[RoleController.getRolelist]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = roleService.getRoleList(requestDTO);
        sendMessages(request, response, data);
    }

    @RequestMapping(value = {"{id}"}, method = RequestMethod.GET)
    public ModelAndView getRole(RequestDTO requestDTO, ModelAndView view,
                                GetRoleReqDTO roleReqDTO) {
        String param = JSON.toJSONString(roleReqDTO);
        log.info("调用[RoleController.getRole]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = roleService.getRole(requestDTO);
        view.addObject("role", data.getResult());
        view.setViewName("/user/roleEdit");
        return view;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public void removeRole(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                           GetRoleReqDTO roleReqDTO) {
        String param = JSON.toJSONString(roleReqDTO);
        log.info("调用[RoleController.removeRole]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = roleService.removeRole(requestDTO);
        sendMessages(request, response, data);

    }
}
